package com.daz.common.dwr;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;

public class DWRScriptSessionListener implements ScriptSessionListener {  
    
   /* 维护一个Map key为session的Id， value为ScriptSession对象  */    
	public static final Map<String, ScriptSession> scriptSessionMap = new HashMap<String, ScriptSession>();  
 
    /** 
     * ScriptSession创建事件 
     */  
    public void sessionCreated(ScriptSessionEvent event) {  
          WebContext webContext = WebContextFactory. get();  
          HttpSession session = webContext.getSession();
          ScriptSession newscriptSession = event.getSession();  
          System. out.println( "session: " + session.getId() + " scriptSession: " + newscriptSession.getId() + "is created!");  
          newscriptSession.setAttribute("userId",session.getAttribute("userId"));
          ScriptSession oldScriptSession = scriptSessionMap.get(session.getId());
          if(oldScriptSession != null){
        	  //oldScriptSession.invalidate();
        	  System.out.println("map中存在了，将销毁，加入新的");
        	  scriptSessionMap.put(session.getId(), newscriptSession);
          }
          else{
        	  System.out.println("map不存在了，加入新的");
        	  scriptSessionMap.put(session.getId(), newscriptSession);
          }
    }  
 
    /** 
     * ScriptSession销毁事件 
     */  
    public void sessionDestroyed(ScriptSessionEvent event) {  
          WebContext webContext = WebContextFactory. get();  
          HttpSession session = webContext.getSession();  
          ScriptSession scriptSession = scriptSessionMap.remove(session.getId());  //移除scriptSession  
         // System. out.println( "session: " + session.getId() + " scriptSession: " + scriptSession.getId() + "is destroyed!");  
    }  
      
    /** 
     * 获取所有ScriptSession 
     */  
    public static Collection<ScriptSession> getScriptSessions(){  
           return scriptSessionMap.values();  
    }  
} 
